﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WingsOverSpace
{
    public enum EnemyType { One, Two, Three, Four, Five };

    class Enemy : WingsDrawableGameComponent
    {
        public Vector2 Velocity { get; set; }
        Texture2D explosion;
        SpriteBatch spriteBatch;
        public EnemyType type;
        long enemyUpdateTicks = 0;
        public bool paused;

        public Enemy(Game g, Vector2 position)
            : base(g)
        {
            Position = position;
            Velocity = new Vector2(0, 0);
            SpriteScaleFactor = 0.5f;
            Visible = false;
            type = EnemyType.One;
            paused = false;
        }

        public void pause()
        {
            paused = true;
        }

        public override void Draw(GameTime gameTime)
        {
            Vector2 offset = new Vector2(sprite.Width / 2, sprite.Height / 2);
                  
            
            switch(ExplodeOrder)
            {
                case ExplosionOrder.NoExplosion:
                    spriteBatch.Begin();
                    spriteBatch.Draw(sprite, boundBox, null, Color.White, 0f, 
                        new Vector2(offset.X, offset.Y), SpriteEffects.None, 0.05f);
                    spriteBatch.End();
                    break;
                default:
                    spriteBatch.Begin();
                    spriteBatch.Draw(explosion, boundBox, null, Color.White, 0f, 
                        new Vector2(offset.X, offset.Y), SpriteEffects.None, 0.05f);
                    spriteBatch.End();
                    break;
             }
        }

        protected override void LoadContent()
        {
            sprite = Game.Content.Load<Texture2D>("EnemyShip001");
            explosion = Game.Content.Load<Texture2D>("EnemyShipExplosion");
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            base.LoadContent();
        }

        public void ChangeType(EnemyType Type)
        {
            switch (Type)
            {
                case EnemyType.One:
                    sprite = Game.Content.Load<Texture2D>("EnemyShip001");
                    break;
                case EnemyType.Two:
                    sprite = Game.Content.Load<Texture2D>("EnemyShip002");
                    break;
                case EnemyType.Three:
                    sprite = Game.Content.Load<Texture2D>("EnemyShip003");
                    break;
                case EnemyType.Four:
                    sprite = Game.Content.Load<Texture2D>("EnemyShip004");
                    break;
                case EnemyType.Five:
                    sprite = Game.Content.Load<Texture2D>("EnemyShip005");
                    break;
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (!paused)
            ++enemyUpdateTicks;

            if (Visible)
            {
                if (ExplodeOrder == ExplosionOrder.EndExplosion)
                {
                    toBeDeleted = true;
                    ExplodeOrder = ExplosionOrder.NoExplosion;
                    return;
                }


                if (ExplodeOrder > ExplosionOrder.NoExplosion)
                {
                    ExplodeOrder++;
                }
                KeyboardState k = Keyboard.GetState();
                float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
                Position += Velocity * elapsedSeconds;
                float totalSeconds = (float)gameTime.TotalGameTime.TotalSeconds;

                if (enemyUpdateTicks > 100)
                {
                    enemyUpdateTicks = 0;

                    //LastShot = totalSeconds;
                    WingsDrawableGameComponent comp = UnusedObject(ComponentType.EnemyShot);
                    if (comp != null)
                    {
                        (comp as EnemyShot).Visible = true;
                        (comp as EnemyShot).Position = new Vector2(Position.X, Position.Y + 90);
                    }
                }

                base.Update(gameTime);
            }
            
        }
                
    }

    class EnemyShot : WingsDrawableGameComponent
    {
        Vector2 Velocity { get; set; }
        SpriteBatch spriteBatch;
        
        public EnemyShot(Game g, Vector2 position)
            : base(g)
        {
            Position = position;
            Velocity = new Vector2(0, 400f);
            SpriteScaleFactor = 0.5f;
            Visible = false;
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            Vector2 offset = new Vector2(sprite.Width / 2, sprite.Height / 2);
            spriteBatch.Begin();
            spriteBatch.Draw(sprite, boundBox, null, Color.White, 0f, 
                new Vector2(offset.X, offset.Y), SpriteEffects.None, 0);
            spriteBatch.End();
        }

        protected override void LoadContent()
        {
            sprite = Game.Content.Load<Texture2D>("possibleLaser");
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            boundBox = new Rectangle((int)(Position.X - SpriteScaleFactor * sprite.Width / (2 * SpriteWidthDivFactor)),
                (int)(Position.Y - SpriteScaleFactor * sprite.Height / (2 * SpriteHeightDivFactor)),
                (int)(SpriteScaleFactor * sprite.Width / SpriteWidthDivFactor),
                (int)(SpriteScaleFactor * sprite.Height / SpriteHeightDivFactor));

            if (Position.Y > 1000)
                toBeDeleted = true;


            if (toBeDeleted == true)
            {
                Visible = false;
                toBeDeleted = false;
            }

            float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
            float totalSeconds = (float)gameTime.TotalGameTime.TotalSeconds;
           // checkForCollision(); 
            Position += Velocity * elapsedSeconds;

            
            base.Update(gameTime);

        }

       // public void checkForCollision()
       // {
         //   foreach (WingsDrawableGameComponent comp in base.Game.Components)
         //   {
           //     if (this != comp)
             //   {
               //     if (comp is Player) //Look for everything that is not a player or a laser
                 //   {
                   //     if (boundBox.Intersects(comp.boundBox))
                     //   {
                       //     this.toBeDeleted = true;

                      //  }
                   // }

               // }
           // }
       // }
    }
}

